package com.ruoyi.model.service.impl;

import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.model.mapper.SModelMapper;
import com.ruoyi.model.domain.SModel;
import com.ruoyi.model.service.ISModelService;

/**
 * 源模型Service业务层处理
 * 
 * @author cfy
 * @date 2025-02-14
 */



        import com.ruoyi.common.config.RuoYiConfig;
        import com.ruoyi.common.utils.StringUtils;
        import com.ruoyi.common.utils.file.FileUploadUtils;
        import org.springframework.web.multipart.MultipartFile;

        @Service
        public class SModelServiceImpl implements ISModelService {
        @Autowired
        private SModelMapper sModelMapper;

        @Override
        public SModel selectSModelById(Long id) {
        return sModelMapper.selectSModelById(id);
        }

        @Override
        public List<SModel> selectSModelList(SModel sModel) {
        return sModelMapper.selectSModelList(sModel);
        }

        @Override
        public int insertSModel(SModel sModel, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
        String filePath = uploadModelFile(file);
        sModel.setModelFile(filePath);
        }
        return sModelMapper.insertSModel(sModel);
        }

        @Override
        public int updateSModel(SModel sModel, MultipartFile file) {
        if (file != null && !file.isEmpty()) {
        // 删除旧文件
        if (StringUtils.isNotEmpty(sModel.getModelFile())) {
        FileUploadUtils.deleteFile(sModel.getModelFile());
        }
        String filePath = uploadModelFile(file);
        sModel.setModelFile(filePath);
        }
        return sModelMapper.updateSModel(sModel);
        }

        @Override
        public int deleteSModelById(Long id) {
        SModel myModel = sModelMapper.selectSModelById(id);
        if (myModel != null && StringUtils.isNotEmpty(myModel.getModelFile())) {
        FileUploadUtils.deleteFile(myModel.getModelFile());
        }
        return sModelMapper.deleteSModelById(id);
        }

        @Override
        public int deleteSModelByIds(Long[] ids) {

        for (Long id : ids) {
        deleteSModelById(id);
        }
        return ids.length;
        }

                private String uploadModelFile(MultipartFile file) {
                        try {
                                // 上传文件路径
                                String filePath = RuoYiConfig.getUploadPath();
                                // 上传并返回新文件名称
                                return FileUploadUtils.upload(filePath, file);
                        } catch (Exception e) {
                                throw new RuntimeException("上传模型文件失败");
                        }
                }

        }